package Client.GUI;


import Client.logic.ClientIF;
import Server.DataBase.Course;
import Server.DataBase.SchoolClass;
import Server.DataBase.Student;
import Server.DataBase.Teacher;
import Server.GUI.MyJTable;
import Server.Message.MessageAllLesson;
import Server.Message.MessageGetAllSchoolClassReplay;
import Server.Message.MessageGetCoursesReplay;
import Server.Message.MessageGetInfo;
import Server.Message.MessageGetStudentByClass;
import Server.Message.MessageLessonReplay;
import Server.Message.MessageGetInfo.TableInfo;
import Server.Message.MessageGetStudentsReplay;
import Server.Message.MessageTeacherReplay;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class ManagerViewInfoPanel extends MyJPanel {


	private static final long serialVersionUID = 1L;
	
	private MyJTable table;
	private ArrayList<Student> arrayStudent;
	private ArrayList<Teacher> arrayTeacher;
	private ArrayList<SchoolClass> arrayClass;
	private ArrayList<Course> arrayCourse;
	private JPanel panelOption;
	private int check=0;
	private Student student;
	private Teacher teacher;
	private SchoolClass sClass;
	private Course course;
	private JLinkButton lnkbtn;
	
	public ManagerViewInfoPanel(ClientIF client) {
		super(PanelType.MANAGER_VIEW_INFO, client);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"view info.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(20, 0, 636, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		init();
	}
		
	
	public void init(){
		setLayout(null);
		initMenu();
		initTable();
		initOption();
	}

	
	public void initMenu(){
	
		JPanel panelMenu = new JPanel();
		panelMenu.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelMenu.setBounds(20, 97, 636, 47);
		add(panelMenu);
		panelMenu.setLayout(new GridLayout(0, 4, 0, 0));
	
		JLinkButton lnkbtnStudents = new JLinkButton();
		panelMenu.add(lnkbtnStudents);
		lnkbtnStudents.setText("Students");
		lnkbtnStudents.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getClient().sendMsgToServer(new MessageGetInfo(TableInfo.STUDENT));
				MessageGetStudentsReplay msg = (MessageGetStudentsReplay) getClient().getMessageFromServer();
				check = 0;
				arrayStudent = msg.getArray();
				StudentTableModel stm = new StudentTableModel();
				stm.setArray(arrayStudent);
				table.setModel(stm);
				studentOption();
				check=1;
			}
		});
		
		JLinkButton lnkbtnTeacher = new JLinkButton();
		panelMenu.add(lnkbtnTeacher);
		lnkbtnTeacher.setText("Teachers");
		lnkbtnTeacher.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getClient().sendMsgToServer(new MessageGetInfo(TableInfo.TEACHER));
				MessageTeacherReplay msg = (MessageTeacherReplay) getClient().getMessageFromServer();
				check = 0;
				arrayTeacher = msg.getArray();
				StudentTableModel stm = new StudentTableModel();
				stm.setArrayT(arrayTeacher);
				table.setModel(stm);
				teacherOption();
				check = 2;
			}
		});
		
		JLinkButton lnkbtnCourse = new JLinkButton();
		lnkbtnCourse.setText("Courses");
		lnkbtnCourse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getClient().sendMsgToServer(new MessageGetInfo(TableInfo.COURSE));
				MessageGetCoursesReplay msg = (MessageGetCoursesReplay) getClient().getMessageFromServer();
				check = 0;
				arrayCourse = msg.getCourseArray();
				CourseTableModel ctm = new CourseTableModel();
				ctm.setArray(arrayCourse);
				table.setModel(ctm);
				CourseOption();
				check = 3;
			}
		});
		panelMenu.add(lnkbtnCourse);
		
	
		JLinkButton lnkbtnClass = new JLinkButton();
		lnkbtnClass.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getClient().sendMsgToServer(new MessageGetInfo(TableInfo.CLASS));
				MessageGetAllSchoolClassReplay msg = (MessageGetAllSchoolClassReplay) getClient().getMessageFromServer();
				check = 0;
				arrayClass = msg.getArray();
				ClassTableModel ctm = new ClassTableModel();
				ctm.setArray(arrayClass);
				table.setModel(ctm);
				ClassOption();
				check = 4;
			}
		});
		panelMenu.add(lnkbtnClass);
		lnkbtnClass.setText("Classes");
	}
	
	public void initOption(){
		panelOption = new JPanel();
		panelOption.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelOption.setBounds(20, 457, 636, 54);
		panelOption.setLayout(new GridLayout(0, 4, 0, 0));
		add(panelOption);
	}
	
	private void studentOption(){
		panelOption.removeAll();
		lnkbtn = new JLinkButton();
		lnkbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getClient().swap(new PersonalFilePanel(getClient(), student));
			}
		});
		panelOption.add(lnkbtn);
		lnkbtn.setText("View Personal File");
		lnkbtn.setEnabled(false);
	}
	
	private void CourseOption(){
		panelOption.removeAll();
		lnkbtn = new JLinkButton();
		lnkbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getClient().sendMsgToServer(new MessageAllLesson(course.getIdCourse()));
				MessageLessonReplay msg = (MessageLessonReplay) getClient().getMessageFromServer();
				
				LessonTableModel ltm = new LessonTableModel();
				ltm.setArray(msg.getArray());
				table.setModel(ltm);
				lnkbtn.setVisible(false);
			}
		});
		panelOption.add(lnkbtn);
		lnkbtn.setEnabled(false);
		lnkbtn.setText("View Lessons");
	}
	
	private void ClassOption(){
		panelOption.removeAll();
		lnkbtn = new JLinkButton();
		lnkbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getClient().sendMsgToServer(new MessageGetStudentByClass(sClass.getIdClass()));
				MessageGetStudentsReplay msg = (MessageGetStudentsReplay) getClient().getMessageFromServer();
				arrayStudent = msg.getArray();
				StudentTableModel stm = new StudentTableModel();
				stm.setArray(arrayStudent);
				check=0;
				table.setModel(stm);
				check=1;
				studentOption();
			}
		});
		panelOption.add(lnkbtn);
		lnkbtn.setEnabled(false);
		lnkbtn.setText("View Students");
	}
	
	private void teacherOption(){
		panelOption.removeAll();
		lnkbtn = new JLinkButton();
		lnkbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getClient().swap(new TeacherInfoPanel(getClient(), teacher));
			}
		});
		panelOption.add(lnkbtn);
		lnkbtn.setEnabled(false);
		lnkbtn.setText("View Info");
	}
	
	public void initTable(){
		table = new MyJTable();
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		table.setBounds(20, 169, 636, 263);
		table.getTable().getSelectionModel().addListSelectionListener(new tableListener());
		add(table);
	}
	
	private class tableListener implements ListSelectionListener{
		public void valueChanged(ListSelectionEvent arg0) {
			try{
				switch (check){
				case 1:
					int s = table.getSelectedItem();
					student = arrayStudent.get(s);
					break;
				case 2:
					int t = table.getSelectedItem();
					teacher = arrayTeacher.get(t);
					break;
				case 3:
					int c = table.getSelectedItem();
					course = arrayCourse.get(c);
					break;
				case 4:
					int sc = table.getSelectedItem();
					sClass = arrayClass.get(sc);
					break;
				}
				lnkbtn.setEnabled(true);
			} catch (java.lang.Throwable t){
			}
		}
	}
	
	

	public MyJPanel pushPanel() {
		return new ManagerViewInfoPanel(getClient());
	}
}
